# Contributor: Edward E. <develinthedetail@gmail.com>

_realname=libheif
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.12.0
pkgrel=7
pkgdesc="HEIF image decoder/encoder library and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/strukturag/libheif"
license=('LGPL-3.0' 'MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-libde265"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686* || \
               ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || \
            echo "${MINGW_PACKAGE_PREFIX}-rav1e" )
         "${MINGW_PACKAGE_PREFIX}-x265")
options=('strip' 'staticlibs')
source=("https://github.com/strukturag/libheif/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-pkgconfig-provide-includedir.patch"
        "002-cmake-enable-gdk-pixbuf.patch"
        "003-cmake-install-headers.patch"::"https://patch-diff.githubusercontent.com/raw/strukturag/libheif/pull/499.diff"
        "004-cmake-install-manpages.patch"::"https://patch-diff.githubusercontent.com/raw/strukturag/libheif/pull/500.diff"
        "005-cmake-install-gnome-thumb.patch"::"https://patch-diff.githubusercontent.com/raw/strukturag/libheif/pull/501.diff")
sha256sums=('e1ac2abb354fdc8ccdca71363ebad7503ad731c84022cf460837f0839e171718'
            'd6bb42bdd884592d1edd3eb69ebc927de4fc329fbc05fab921b0aee23b68b124'
            '51012f7d84bd3d177839d99aaae2df388e42bd51a95bceac4329be81358895e2'
            '8944e9953b2924b581cf688a3dc7483cbd0f208a24dda81963e70a45db16fd48'
            '02e026708b01f2a86c6ddb9346fb7bd4cd9e43b5c0a598116255f2bdb1e5cc74'
            '38ae18671817313c58f7c33e337c1f82aaf483c07e9fda541750965d99ee6f07')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/001-fix-pkgconfig-provide-includedir.patch"
  patch -Np1 -i "${srcdir}/002-cmake-enable-gdk-pixbuf.patch"
  patch -Np1 -i "${srcdir}/003-cmake-install-headers.patch"
  patch -Np1 -i "${srcdir}/004-cmake-install-manpages.patch"
  patch -Np1 -i "${srcdir}/005-cmake-install-gnome-thumb.patch"
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Keep "-DX265_API_IMPORTS" flag due to https://github.com/strukturag/libheif/issues/357
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DX265_CFLAGS="-DX265_API_IMPORTS" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # Build pkgconfig file using autoconf because
  # CMake generates it without ${prefix} variable

  [[ -d "${srcdir}"/build-${CARCH}-pkgconfig ]] && rm -rf "${srcdir}"/build-${CARCH}-pkgconfig
  mkdir -p "${srcdir}"/build-${CARCH}-pkgconfig && cd "${srcdir}"/build-${CARCH}-pkgconfig

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}
}

package() {
  cd "${srcdir}"/build-${CARCH}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  cp -v "${srcdir}"/build-${CARCH}-pkgconfig/libheif.pc "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/examples/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-examples"
}
